//
//  UIViewController+ShortTitle.m
//  YDGJ
//
//  Created by ydcq on 16/5/17.
//  Copyright © 2016年 Galaxy360. All rights reserved.
//

#import "UIViewController+ShortTitle.h"
#import "JRSwizzle.h"
#import <objc/runtime.h>

@implementation UIViewController (ShortTitle)

+ (void)load
{
    NSError *error;
    [[self class] jr_swizzleMethod:@selector(setTitle:) withMethod:@selector(SY_setTitle:) error:&error];
    NSLog(@"%@",error);
    
    [[self class] jr_swizzleMethod:@selector(viewWillAppear:) withMethod:@selector(SY_viewWillAppear:) error:&error];
    NSLog(@"%@",error);
    
    [[self class] jr_swizzleMethod:@selector(viewWillDisappear:) withMethod:@selector(SY_viewWillDisappear:) error:&error];
    NSLog(@"%@",error);
}

- (void)SY_setTitle:(NSString *)title
{
    CGFloat width = [title widthWithFont:FontCustomSize(17) constrainedToHeight:21];
    if (width>=(Screen_Width-38*2)) {
        int maxLen = (Screen_Width-38*2)/17.f;
        title = [title substringToIndex:(maxLen-2)];
        title = [title stringByAppendingString:@"..."];
    }
    [self SY_setTitle:title];
}

- (void)SY_viewWillAppear:(BOOL)animated
{
    [self SY_viewWillAppear:animated];
    if (![self.superclass isSubclassOfClass:NSClassFromString(@"GJBaseViewController")]) {
        return;
    }
//    [MobClick beginLogPageView:NSStringFromClass([self class])];
}

- (void)SY_viewWillDisappear:(BOOL)animated
{
    [self SY_viewWillDisappear:animated];
    if (![self.superclass isSubclassOfClass:NSClassFromString(@"GJBaseViewController")]) {
        return;
    }
//    [MobClick endLogPageView:NSStringFromClass([self class])];
}

@end
